const express = require('express');
const router = express.Router();
const path = require('path');
const fse = require('fs-extra');
const http = require('http');
const readline = require('readline');
const { automationDownload, convertToTimestamp, compareTime } = require('../utils/utils');
const indexService = require('../service/index.service');

/* GET home page. */
router.get('/download', async function (req, res, next) {
  automationDownload();
  res.send('okok!');
});

router.get('/import', function (req, res, next) {
  const filePath = 'public/jsonl/2023beforedata.jsonl';

  const fileStream = fse.createReadStream(filePath);
  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  const jsonData = []; // 存储解析后的JSON对象
  rl.on('line', (line) => {
    const toJson = JSON.parse(line);
    if (compareTime(toJson.BasicInfo.CreateTime, '2020-01-01 00:00:00', '2020-12-31 23:59:59')) {
      const StrArr = [toJson.FileId, convertToTimestamp(toJson.BasicInfo.CreateTime), toJson.BasicInfo.MediaUrl, toJson.BasicInfo.Type];
      jsonData.push(StrArr);
    }
  });

  rl.on('close', () => {
    // 在这里处理jsonData数组，即包含所有JSON对象的数组
    indexService.batchInsertMoments(jsonData);
    res.send(`ok::== ${jsonData.length}`); // 将jsonData作为响应发送给客户端
  });
});

module.exports = router;
